package com.xxb.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xxb.entity.Type;
import com.xxb.util.DBHelp;
import com.xxb.util.Rowmapper;

public class TypeDao {
	
	private DBHelp<Type> db = new DBHelp<Type>();
	
	public int save(Type type){
		String sql = "insert into t_type(id,typename) values(?,?)";
		return db.executeSQL(sql, type.getId(),type.getTypename());
	}
	public List<Type> finAll(){
		String sql = "select id,typename from t_type ";
		return db.quertyList(sql, new TypeRowmapper());
	}
	
	public Type findByid(int id){
		String sql = "select id,typename from t_type where id = ?";
		return db.queryObject(sql, new TypeRowmapper(),id);
		
	}
	
	public int update(Type type){
		String sql = "update t_type set typename=? where id=?";
		return db.executeSQL(sql,type.getTypename(),type.getId());
	}
	
	public int deleteByid(int id){
		String sql = "delete from t_type where id=?";
		return db.executeSQL(sql, id);
		
	}
	private class TypeRowmapper implements Rowmapper<Type>{

		public Type mapRow(ResultSet rs) throws SQLException {
			Type type = new Type();
			
			type.setId(rs.getInt("id"));
			type.setTypename(rs.getString("typename"));
			
			return type;
		}
	}
}
